package com.ruibang.glass.equipment.service.impl.device;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.DeviceChildReq;
import com.ruibang.glass.equipment.dto.DeviceAllChildDTO;
import com.ruibang.glass.equipment.dto.DeviceChildDTO;
import com.ruibang.glass.equipment.entity.device.DeviceChild;
import com.ruibang.glass.equipment.mapper.device.DeviceChildMapper;
import com.ruibang.glass.equipment.service.DeviceChildService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备子层级服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Slf4j
@Service
public class DeviceChildServiceImpl extends ServiceImpl<DeviceChildMapper, DeviceChild> implements DeviceChildService {
    /**
     * 获取子设备集合 集合
     * @param deviceChild1
     * @return
     */
    @Override
    public DeviceAllChildDTO getChildList(DeviceChild deviceChild1) {
        List<DeviceChild> deviceChildAllList = new ArrayList<>();
        if (StringUtils.isBlank(deviceChild1.getDeviceChild().getDeviceId())) {
            deviceChildAllList = baseMapper.selectList(Wrappers.<DeviceChild>lambdaQuery().eq(DeviceChild::getIsDelete,Constants.ZERO_STR).eq(DeviceChild::getDeviceId, deviceChild1.getDeviceId()));
        } else {
            deviceChildAllList = baseMapper.selectList(Wrappers.<DeviceChild>lambdaQuery().eq(DeviceChild::getIsDelete,Constants.ZERO_STR).eq(DeviceChild::getDeviceId, deviceChild1.getDeviceChild().getDeviceId()));
        }

        List<DeviceChild> pageList = deviceChildAllList;
        List<DeviceChildDTO> areaManageDTOS = new ArrayList<>();
        for (DeviceChild deviceChild : pageList) {
            DeviceChildDTO areaManageDTO = new DeviceChildDTO();
            BeanUtils.copyProperties(deviceChild, areaManageDTO);
            areaManageDTO.setChildNumber(deviceChild.getChildNumber());
            areaManageDTOS.add(areaManageDTO);
        }
        List<String> parentsIdList = pageList.stream().map(DeviceChild::getId).collect(Collectors.toList());
        getAllChidList(areaManageDTOS, deviceChildAllList, parentsIdList);
        List<DeviceChildDTO> areaManageDTOS1 = cleanSource(areaManageDTOS);
        DeviceAllChildDTO deviceAllChildDTO = new DeviceAllChildDTO();
        deviceAllChildDTO.setDeviceChild(deviceChildAllList);
        deviceAllChildDTO.setDeviceChildDTO(areaManageDTOS1);
        return deviceAllChildDTO;
    }

    /**
     * 全量子设备树结构
     * @param childReq
     * @return
     */
    @Override
    public List<DeviceChild> getNewChildList(DeviceChildReq childReq) {
        final LambdaQueryWrapper<DeviceChild> lambda = new QueryWrapper<DeviceChild>().lambda();
        if(childReq!=null){
            if(StringUtils.isNotBlank(childReq.getParentId())){
                lambda.eq(DeviceChild::getParentId,childReq.getParentId());
            }else{
                lambda.eq(DeviceChild::getParentId,0);
            }
            if(StringUtils.isNotBlank(childReq.getDeviceId())){
                lambda.eq(DeviceChild::getDeviceId,childReq.getDeviceId());
            }
            if(childReq.getLevel()!=null){
                lambda.eq(DeviceChild::getLevel,childReq.getLevel());
            }
        }
        return baseMapper.selectList(lambda);
    }

    @Override
    public void addDeviceChild(DeviceChild deviceChild) {
        if (StringUtils.isBlank(deviceChild.getParentId()) || deviceChild.getParentId().equals(Constants.ZERO_STR)) {
            deviceChild.setLevel(Constants.ONE_INT);
            baseMapper.insert(deviceChild);
            return;
        }
        baseMapper.insert(deviceChild);
    }


    //父层级获取所有子层级
    public void getAllChidList(List<DeviceChildDTO> areaManageDTOS, List<DeviceChild> deviceChildAllList, List<String> parentsIdList) {
        List<DeviceChildDTO> newList = new ArrayList<>();
        for (DeviceChild deviceChild : deviceChildAllList) {
            for (String id : parentsIdList) {
                if (deviceChild.getParentId().equals(id)) {
                    DeviceChildDTO areaManageDTO = new DeviceChildDTO();
                    areaManageDTO.setId(deviceChild.getId());
                    areaManageDTO.setChildName(deviceChild.getChildName());
                    areaManageDTO.setChildNumber(deviceChild.getChildName());
                    areaManageDTO.setQuantity(deviceChild.getQuantity());
                    areaManageDTO.setUnit(deviceChild.getUnit());
                    areaManageDTO.setLevel(deviceChild.getLevel());
                    newList.add(areaManageDTO);
                }
            }
        }
        if (org.springframework.util.CollectionUtils.isEmpty(newList)) {
            return;
        }
        areaManageDTOS.addAll(newList);
        List<String> collect = newList.stream().map(DeviceChildDTO::getId).collect(Collectors.toList());
        getAllChidList(areaManageDTOS, deviceChildAllList, collect);
    }

    // 数据组装
    private List<DeviceChildDTO> cleanSource(List<DeviceChildDTO> areaManageDTOS) {
        Map<Integer, List<DeviceChildDTO>> areaMap = areaManageDTOS.stream().sorted(Comparator.comparing(DeviceChildDTO::getLevel)).collect(Collectors.groupingBy(DeviceChildDTO::getLevel));
        List<Integer> levelList = areaManageDTOS.stream().sorted(Comparator.comparing(DeviceChildDTO::getLevel).reversed()).map(DeviceChildDTO::getLevel).distinct().collect(Collectors.toList());
        List<DeviceChildDTO> levelAllList = new ArrayList<>();
        for (Integer level : levelList) {
            List<DeviceChildDTO> levelOneList = areaMap.get(level);
            if (CollectionUtil.isEmpty(levelAllList)) {
                List<DeviceChildDTO> collect = levelOneList.stream().sorted(Comparator.comparing(DeviceChildDTO::getChildNumber)).collect(Collectors.toList());
                levelAllList.addAll(collect);
            } else {
                for (DeviceChildDTO areaManageDTO : levelOneList) {
                    for (DeviceChildDTO manageDTO : levelAllList) {
                        if (areaManageDTO.getId().equals(manageDTO.getParentId())) {
                            if (org.springframework.util.CollectionUtils.isEmpty(areaManageDTO.getChildren())) {
                                List<DeviceChildDTO> addChidren = new ArrayList<>();
                                addChidren.add(manageDTO);
                                List<DeviceChildDTO> collect = addChidren.stream().sorted(Comparator.comparing(DeviceChildDTO::getChildNumber)).collect(Collectors.toList());
                                areaManageDTO.setChildren(collect);
                            } else {
                                List<DeviceChildDTO> addChidren = areaManageDTO.getChildren();
                                addChidren.add(manageDTO);
                                List<DeviceChildDTO> collect = addChidren.stream().sorted(Comparator.comparing(DeviceChildDTO::getChildNumber)).collect(Collectors.toList());
                                areaManageDTO.setChildren(collect);
                            }
                        }
                    }
                }
                levelAllList = new ArrayList<>();
                levelAllList.addAll(levelOneList);
            }
        }
        return levelAllList;
    }

}
